﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using NetJsWire.JsonRpcHandler;
using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.JsonRpcHandler.Util;
using NetJsWire.Widgets.Config;

namespace NetJsWire.Widgets.Ui.List
{
    [Renderer( typeof( Renderers.List.ListView ) )]
    public class ListView : DataGridView, IWidget
    {
        private readonly IRenderer _renderer;
        private IConfigOptions _configOptions;

        public ListView()
        {
            ReadOnly = true;
            _renderer = Helper.GetRenderer( this );
        }

        public ListView( IContainer container )
            : this()
        {
            container.Add( this );
        }

        #region Hidden properties from base

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( null )]
        public new string AccessibleDescription { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( null )]
        public new string AccessibleName { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( null )]
        public new string AccessibleRole { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( false )]
        public override bool AllowDrop { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( null )]
        public override ContextMenu ContextMenu { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( false )]
        public new bool AllowUserToAddRows { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( false )]
        public new bool AllowUserToDeleteRows { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( false )]
        public new bool AllowUserToOrderColumns { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( false )]
        public new bool AllowUserToResizeColumns { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( false )]
        public new bool AllowUserToResizeRows { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( null )]
        public new DataGridViewCellStyle AlternatingRowsDefaultCellStyle { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( ( DataGridViewAutoSizeColumnsMode ) 1 )]
        public new DataGridViewAutoSizeColumnsMode AutoSizeColumnsMode { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( ( DataGridViewAutoSizeRowsMode ) 0 )]
        public new DataGridViewAutoSizeRowsMode AutoSizeRowsMode { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( typeof( Color ), "AppWorkspace" )]
        public new Color BackgroundColor { get { return base.BackgroundColor; } set { ; } }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( ( BorderStyle ) 0 )]
        public new BorderStyle BorderStyle { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( ( DataGridViewCellBorderStyle ) 0 )]
        public new DataGridViewCellBorderStyle CellBorderStyle { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( false )]
        public new bool CausesValidation { get; set; }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( ( DataGridViewClipboardCopyMode ) 0 )]
        public new DataGridViewClipboardCopyMode ClipboardCopyMode { get; set; }

        #endregion

        #region Implementation of IWidget

        ///<summary>
        ///  The renderer is the responsible for representing the object in the client side.
        ///</summary>
        public IRenderer Renderer
        {
            get { return _renderer; }
        }

        /// <summary>
        ///   Add a client event to the Widget in the form of: Windget.on('name', function);
        /// </summary>
        /// <param name = "name">The event name.</param>
        /// <param name = "function">A function name or inline code to execute.</param>
        public void On( string name, string function )
        {
            Renderer.AddClientEvent( name, function );
        }

        /// <summary>
        ///   Adds a CSS class to the component's underlying element.
        /// </summary>
        /// <param name = "cls">The CSS class name to add.</param>
        public void AddClass( string cls )
        {
            Renderer.CallCommand( "addClass", cls );
        }

        /// <summary>
        ///   Removes a CSS class from the component's underlying element.
        /// </summary>
        /// <param name = "cls">The CSS class name to remove.</param>
        public void RemoveClass( string cls )
        {
            Renderer.CallCommand( "removeClass", cls );
        }

        /// <summary>
        ///   If the framework should generate a global Javascript variable that points to the Widget, the name of the variable will be the same as ClientID property of the Renderer.
        /// </summary>
        /// <remarks>
        ///   Only do this if its really necesary because every global property will stay there until you manually delete it, the framework wont delete globals, is up to the developer to do that.
        /// </remarks>
        [DefaultValue( false )]
        [Description( "If the framework should generate a global Javascript variable that points to the Widget, the name of the variable will be the same as ClientID property of the Renderer." )]
        public bool GenerateClientMember { get; set; }

        /// <summary>
        ///   The ConfigOptions that will be passed to Ext when constructing the widget.
        /// </summary>
        [RefreshProperties( RefreshProperties.All ),
         DesignerSerializationVisibility( DesignerSerializationVisibility.Content ), Category( "Config" )]
        public IConfigOptions ConfigOptions
        {
            get { return _configOptions ?? ( _configOptions = new ListViewConfigOptions( this ) { Xtype = "listview" } ); }
        }

        #endregion
    }
}
